package org.niuke.linkedlist;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    public ListNode addInList (ListNode head1, ListNode head2) {


        ListNode c1 = re(head1);;
        ListNode c2 = re(head2);;
        int shi = 0;
        ListNode head = null;
        while (c1 != null || c2 != null || shi != 0) {
            int n1 = c1 == null ? 0 : c1.val;
            int n2 = c2 == null ? 0 : c2.val;
            int num = n1 + n2 + shi;
            int ge = num % 10;
            shi = num / 10;
            ListNode temp = new ListNode(ge);
            temp.next = head;
            head = temp;
            if (c1 != null) {
                c1 = c1.next;
            }
            if (c2 != null) {
                c2 = c2.next;
            }
        }
        return head;



    }

    private ListNode re(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}